<template>
  <div>
    <Card>
      <div class="mb_10">
        <Button @click="addParent" icon="md-add">添加一级分类</Button>
      </div>
      <Table class="table" :load-data="handleLoadData" row-key="catId" :loading="loading" :data="tableData"
        :columns="columns">
        <template slot="action" slot-scope="scope">
          <Dropdown v-show="scope.row.catLevel == 3" trigger="click">
            <Button size="small">
              绑定
              <Icon type="ios-arrow-down"></Icon>
            </Button>
            <DropdownMenu slot="list">
              <DropdownItem @click.native="brandOperation(scope.row.catId)">编辑绑定品牌</DropdownItem>
              <DropdownItem @click.native="specOperation(scope.row)">添加规格</DropdownItem>
              <!-- <DropdownItem @click.native="parameterOperation(scope.row)">编辑绑定参数</DropdownItem> -->
            </DropdownMenu>
          </Dropdown>

          &nbsp;
          <Dropdown trigger="click">
            <Button size="small">
              操作
              <Icon type="ios-arrow-down"></Icon>
            </Button>
            <DropdownMenu slot="list">
              <DropdownItem @click.native="edit(scope.row)">编辑</DropdownItem>
              <DropdownItem v-if="scope.row.showStatus == false" @click.native="enable(scope.row)">启用</DropdownItem>
              <DropdownItem v-if="scope.row.showStatus == true" @click.native="disable(scope.row)">禁用</DropdownItem>
              <DropdownItem @click.native="remove(scope.row)">删除</DropdownItem>
            </DropdownMenu>
          </Dropdown>
          &nbsp;
          <Button v-show="scope.row.catLevel != 3" type="primary" @click="addChildren(scope.row)" size="small"
            icon="md-add" style="margin-right: 5px">添加子分类
          </Button>
        </template>

        <template slot="commissionRate" slot-scope="scope">
          {{ scope.row.commissionRate }}%
        </template>

        <template slot="showStatus" slot-scope="{ row }">
          <Tag :class="{ ml_10: row.showStatus }" :color="row.showStatus == true ? 'success' : 'error'">
            {{ row.showStatus == true ? "正常启用" : "禁用" }}</Tag>
        </template>
      </Table>

      <Modal :title="modalTitle" v-model="modalVisible" :mask-closable="false" :width="500">
        <Form ref="form" :model="formAdd" :label-width="100" :rules="formValidate">
          <div v-if="showParent">
            <FormItem label="上级分类" prop="parentId">
              {{ parentTitle }}
              <Input v-model="formAdd.parentId" clearable style="width: 100%; display: none" />
            </FormItem>
          </div>
          <FormItem label="层级" prop="catLevel" style="display: none">
            <Input v-model="formAdd.catLevel" clearable style="width: 100%" />
          </FormItem>
          <FormItem label="分类名称" prop="name">
            <Input v-model="formAdd.name" clearable style="width: 100%" />
          </FormItem>
          <FormItem label="分类图标" prop="image" v-if="formAdd.catLevel !== 1">
            <upload-pic-input v-model="formAdd.image" style="width: 100%"></upload-pic-input>
          </FormItem>
          <FormItem label="排序值" prop="sortOrder" style="width: 345px">
            <InputNumber v-model="formAdd.sortOrder"></InputNumber>
          </FormItem>
          <!-- <FormItem label="佣金比例(%)" prop="commissionRate" style="width: 345px">
            <InputNumber v-model="formAdd.commissionRate"></InputNumber>
          </FormItem> -->
          <FormItem label="是否启用" prop="showStatus">
            <i-switch size="large" v-model="formAdd.showStatus" :true-value="0" :false-value="1">
              <span slot="open">启用</span>
              <span slot="close">禁用</span>
            </i-switch>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="modalVisible = false">取消</Button>
          <Button type="primary" :loading="submitLoading" @click="Submit">提交</Button>
        </div>
      </Modal>

      <Modal :title="modalBrandTitle" v-model="modalBrandVisible" :mask-closable="false" :width="500">
        <Form ref="brandForm" :model="brandForm" :label-width="100">
          <Select v-model="brandForm.categoryBrands" filterable>
            <Option v-for="item in brandWay" :value="item.brandId" :key="item.brandId">{{
                item.name
            }}</Option>
          </Select>
        </Form>
        <div slot="footer">
          <Button type="text" @click="modalBrandVisible = false">取消</Button>
          <Button type="primary" :loading="submitLoading" @click="saveCategoryBrand">提交</Button>
        </div>
      </Modal>

      <Modal :title="modalSpecTitle" v-model="modalSpecVisible" :mask-closable="false" :width="500">
        <Form ref="form" :model="form" :label-width="100" :rules="formValidate">
          <FormItem label="规格名称" prop="specName">
            <Input v-model="form.specName" maxlength="30" clearable style="width: 100%" />
          </FormItem>
          <FormItem label="规格值" prop="specValue">
            <Select v-model="form.specValue" placeholder="输入后回车添加" multiple filterable allow-create
              :popper-append-to-body="false" popper-class="spec-values-popper"
              style="width: 100%; text-align: left; margin-right: 10px">
              <Option v-for="item in specValue" :value="item" :label="item" :key="item">
              </Option>
            </Select>
          </FormItem>
          <Form-item label="规格类型" prop="attrType">
            <Select v-model="form.attrType" clearable style="width:200px">
              <Option v-for="item in attrtp" :value="item.value" :key="item.value">{{ item.label }}</Option>
            </Select>
          </Form-item>
        </Form>
        <div slot="footer">
          <Button type="text" @click="modalVisible = false">取消</Button>
          <Button type="primary" :loading="submitLoading" @click="saveSpec">提交</Button>
        </div>
      </Modal>
    </Card>
  </div>
</template>
<script>
import {
  insertCategory,
  updateCategory,
  getBrandListData,
  delCategory,
  getCategoryBrandListData,
  saveCategoryBrand,
  getSpecificationList,
  getCategorySpecListData,
  disableCategory,
  saveCategorySpec,
  getCategoryTree,
} from "@/api/goods";
import uploadPicInput from "@/views/my-components/lili/upload-pic-input";
import { regular } from "@/utils";
import * as ApiCate from "@/api/myapi/Cate"
import * as ApiBrand from "@/api/myapi/Brand"
import * as ApiAttr from "@/api/myapi/Attr"
export default {
  name: "goods-category",
  components: {
    uploadPicInput,
  },
  data() {
    return {
      attrtp: [
        {
          value: "0",
          label: "销售属性"
        },
        {
          value: "1",
          label: "基本属性"
        }
      ],
      submitLoading: false, //加载状态
      categoryList: [], // 分类列表
      loading: false, // 加载状态
      brands: [], //品牌集合
      specifications: [], //规格集合
      categoryId: "", // 分类id
      categorySpecs: [], //已经选择的规格
      modalType: 0, // 添加或编辑标识
      modalVisible: false, // 添加或编辑显示
      modalBrandVisible: false, //品牌关联编辑显示
      modalSpecVisible: false, //品牌关联编辑显示
      modalTitle: "", // 添加或编辑标题
      showParent: false, // 是否展示上级菜单
      parentTitle: "", // 父级菜单名称
      modalBrandTitle: "", // 品牌弹窗标题
      modalSpecTitle: "", // 规格弹窗标题
      formAdd: {
        // 添加或编辑表单对象初始化数据
        parentId: "",
        name: "",
        image: "",
        sortOrder: 0,
        // deleteFlag: 0,
        showStatus: 0,
        // commissionRate: 0,
        catLevel: 1,
      },
      brandForm: {
        categoryBrands: [],
      },
      brandWay: "", //请求绑定品牌的信息
      specForm: {}, // 规格数据
      // 表单验证规则
      formValidate: {
        // commissionRate: [regular.REQUIRED, regular.INTEGER],
        name: [regular.REQUIRED, regular.VARCHAR20],
        sortOrder: [regular.REQUIRED, regular.INTEGER],
      },
      columns: [
        {
          title: "分类名称",
          key: "name",
          tree: true,
        },
        {
          title: "状态",
          // slot: "deleteFlag",
          slot: "showStatus",
        },
        // {
        //   title: "佣金",
        //   key: "commissionRate",

        //   slot: "commissionRate",
        // },
        {
          title: "操作",
          key: "action",

          slot: "action",
        },
      ],
      tableData: [], // 表格数据
      categoryIndex: 0, // 分类id
      checkedCategoryChildren: "", //选中的分类子级
      // 表单验证规则
      formValidate: {
        specName: [
          regular.REQUIRED,
          // regular.VARCHAR20
        ],
        specValue: [
          regular.REQUIRED,
          regular.VARCHAR255
        ],
        catelogId: [
          regular.REQUIRED,
        ],
        attrType: [
          regular.REQUIRED,
        ]
      },
      form: {
        // 添加或编辑表单对象初始化数据
        specName: "",
        specValue: "",
        catelogId: null,
        attrType: null,
      },
    };
  },
  methods: {
    // 初始化数据
    init() {
      this.getAllList(0);
      this.getBrandList();
      this.getSpecList();
    },
    //获取所有品牌
    getBrandList() {
      // getBrandListData().then((res) => {
      //   this.brandWay = res;
      // });
      ApiBrand.getAllBrand().then(res => {
        if (res.data.code == 201) {
          this.brandWay = res.data.obj
        }
      })
    },
    //获取所有规格
    getSpecList() {
      getSpecificationList().then((res) => {
        if (res.length != 0) {
          this.specifications = res;
        }
      });
    },
    //弹出品牌关联框
    brandOperation(v) {
      // getCategoryBrandListData(v.id).then((res) => {
      //   this.categoryId = v.id;
      //   this.modalBrandTitle = "品牌关联";
      //   this.brandForm.categoryBrands = res.result.map((item) => item.id);
      //   this.modalBrandVisible = true;
      // });
      ApiBrand.getCateBrandRelation(v).then(res => {
        if (res.data.code == 201) {
          this.categoryId = v;
          this.modalBrandTitle = "品牌关联";
          // this.brandForm.categoryBrands = res.data.obj.map((item) => item.id);
          // this.brandForm.categoryBrands = res.data.obj
          this.modalBrandVisible = true;
        }
      })
    },
    //弹出规格关联框
    specOperation(v) {
      console.log('测试点击弹出规格关联框', v);
      this.categoryId = v.catId;
      this.modalSpecTitle = "规格添加";
      this.modalSpecVisible = true;
      // getCategorySpecListData(v.id).then((res) => {
      //   this.categoryId = v.id;
      //   this.modalSpecTitle = "规格关联";
      //   this.specForm.categorySpecs = res.map((item) => item.id);
      //   this.modalSpecVisible = true;
      // });
    },
    //保存分类规格绑定
    saveCategorySpec() {
      saveCategorySpec(this.categoryId, this.specForm).then((res) => {
        this.submitLoading = false;
        if (res.success) {
          this.$Message.success("操作成功");
          this.modalSpecVisible = false;
        }
      });
    },
    //保存分类品牌绑定
    saveCategoryBrand() {
      let params = {
        brandId: this.brandForm.categoryBrands,
        cateId: this.categoryId
      }
      ApiCate.addCateBrandRelation(params).then(res => {
        this.submitLoading = false;
        if (res.data.code == 201) {
          this.$Message.success("操作成功");
          this.modalBrandVisible = false;
        } else {
          this.$Message.error(res.data.message);
          this.modalBrandVisible = false;
        }
      })
      // saveCategoryBrand(this.categoryId, this.brandForm).then((res) => {
      //   this.submitLoading = false;
      //   if (res.success) {
      //     this.$Message.success("操作成功");
      //     this.modalBrandVisible = false;
      //   }
      // });
    },
    // 编辑绑定参数
    parameterOperation(v) {
      this.$router.push({ name: "parameter", query: { id: v.id } });
    },
    // 添加子分类
    addChildren(v) {
      this.modalType = 0;
      this.modalTitle = "添加子分类";
      this.parentTitle = v.name;
      this.formAdd.catLevel = eval(v.catLevel + "+1");
      // this.formAdd.commissionRate = v.commissionRate;
      this.showParent = true;
      delete this.formAdd.id;
      this.formAdd.parentId = v.catId;
      this.modalVisible = true;
    },
    // 编辑分类
    edit(v) {
      console.log('测试编辑', v);
      this.modalType = 1;
      this.modalTitle = "编辑";
      this.formAdd.id = v.catId;
      this.formAdd.name = v.name;
      this.formAdd.catLevel = v.catLevel;
      this.formAdd.parentId = v.parentCid;
      this.formAdd.sortOrder = v.sort;
      // this.formAdd.commissionRate = v.commissionRate;
      this.formAdd.showStatus = v.showStatus;
      this.formAdd.image = v.icon;
      this.showParent = false;
      this.modalVisible = true;
    },
    // 添加一级分类
    addParent() {
      this.modalType = 0;
      this.modalTitle = "添加一级分类";
      this.parentTitle = "顶级分类";
      this.showParent = true;
      this.$refs.form.resetFields();
      delete this.formAdd.id;
      this.formAdd.parentId = 0;
      this.modalVisible = true;
    },
    // 提交
    Submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          if (this.modalType === 0) {
            // 添加 避免编辑后传入id等数据 记得删除
            delete this.formAdd.id;
            console.log('测试参数:', this.formAdd);
            ApiCate.addCate(this.formAdd).then(res => {
              this.submitLoading = false;
              if (res.data.code == 201) {
                this.$Message.success("添加成功");
                this.getAllList(0);
                this.modalVisible = false;
                this.$refs.form.resetFields();
              }
            })

            // insertCategory(this.formAdd).then((res) => {
            //   this.submitLoading = false;
            //   if (res.success) {
            //     this.$Message.success("添加成功");
            //     this.getAllList(this.categoryIndex);
            //     this.modalVisible = false;
            //     this.$refs.form.resetFields();
            //   }
            // });
          } else {
            // 编辑
            console.log('测试编辑:', this.formAdd);
            ApiCate.editCate(this.formAdd).then(res => {
              this.submitLoading = false;
              if (res.data.code == 201) {
                this.$Message.success("修改成功");
                this.getAllList(this.categoryIndex);
                this.modalVisible = false;
                this.$refs.form.resetFields();
              } else {
                this.$Message.error(res.data.message);
                this.getAllList(this.categoryIndex);
                this.modalVisible = false;
                this.$refs.form.resetFields();
              }
            })
            // updateCategory(this.formAdd).then((res) => {
            //   this.submitLoading = false;
            //   if (res.success) {
            //     this.$Message.success("修改成功");
            //     this.getAllList(this.categoryIndex);
            //     this.modalVisible = false;
            //     this.$refs.form.resetFields();
            //   }
            // });
          }
        }
      });
    },
    // 删除分类
    remove(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除 " + v.name + " ?",
        loading: true,
        onOk: () => {
          // 删除
          delCategory(v.id).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("操作成功");
              this.getAllList(0);
            }
          });
        },
      });
    },

    // 异步手动加载分类名称
    handleLoadData(item, callback) {
      if (item.catLevel == 1) {
        let categoryList = JSON.parse(JSON.stringify(this.categoryList));
        categoryList.forEach((val) => {
          if (val.catId == item.catId) {
            val.children.map((child) => {
              child._loading = false;
              child.children = [];
            });
            // 模拟加载
            setTimeout(() => {
              callback(val.children);
            }, 1000);
          }
        });
      } else {
        this.deepCategoryChildren(item.catId, this.categoryList);
        setTimeout(() => {
          callback(this.checkedCategoryChildren);
        }, 1000);
      }
    },

    // 通过递归children来实现手动加载数据
    deepCategoryChildren(catId, list) {
      if (catId != "1" && list.length != 0) {
        for (let i = 0; i < list.length; i++) {
          let item = list[i];
          if (item.catId == catId) {
            this.checkedCategoryChildren = item.children;
            return;
          } else {
            this.deepCategoryChildren(catId, item.children);
          }
        }
      }
    },
    // 获取分类数据
    getAllList(parent_id) {
      this.loading = true;
      ApiCate.listWithTree().then(res => {
        this.loading = false;
        if (res.data.code == 201) {
          localStorage.setItem("category", JSON.stringify(res.data.obj));
          this.categoryList = JSON.parse(JSON.stringify(res.data.obj));
          this.tableData = res.data.obj.map((item) => {
            if (item.children.length != 0) {
              item.children = [];
              item._loading = false;
            }
            return item;
          });
        }
      });
      // getCategoryTree(parent_id).then((res) => {
      //   this.loading = false;
      //   if (res.success) {
      //     localStorage.setItem("category", JSON.stringify(res.result));
      //     this.categoryList = JSON.parse(JSON.stringify(res.result));
      //     this.tableData = res.result.map((item) => {
      //       if (item.children.length != 0) {
      //         item.children = [];
      //         item._loading = false;
      //       }
      //       return item;
      //     });
      //   }
      // });
    },
    // 启用分类
    enable(v) {
      console.log('测试启用按钮:', v);
      this.$Modal.confirm({
        title: "确认启用",
        content: "您是否要启用当前分类 " + v.name + " 及其子分类?",
        loading: true,
        okText: "是",
        cancelText: "否",
        onOk: () => {
          // disableCategory(v.id, { enableOperations: 0 }).then((res) => {
          //   this.$Modal.remove();
          //   if (res.success) {
          //     this.$Message.success("操作成功");
          //     this.getAllList(0);
          //   }
          // });
          ApiCate.enableCate(v.catId).then(res => {
            this.$Modal.remove();
            if (res.data.code == 201) {
              this.$Message.success("操作成功");
              this.getAllList(0);
            } else {
              this.$Message.error(res.data.message);
              this.getAllList(0);
            }
          })
        },
        onCancel: () => {
          this.getAllList(0);
        },
      });
    },
    // 禁用分类
    disable(v) {
      console.log('测试禁用按钮:', v);
      this.$Modal.confirm({
        title: "确认禁用",
        content: "您是否要禁用当前分类 " + v.name + " 及其子分类?",
        loading: true,
        okText: "是",
        cancelText: "否",
        onOk: () => {
          // disableCategory(v.id, { enableOperations: true }).then((res) => {
          //   this.$Modal.remove();
          //   if (res.success) {
          //     this.$Message.success("操作成功");
          //     this.getAllList(0);
          //   }
          // });
          ApiCate.disableCate(v.catId).then(res => {
            this.$Modal.remove();
            if (res.data.code == 201) {
              this.$Message.success("操作成功");
              this.getAllList(0);
            } else {
              this.$Message.error(res.data.message);
              this.getAllList(0);
            }
          })
        },
        onCancel: () => {
          this.getAllList(0);
        },
      });
    },
    //新增规格
    saveSpec() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          // 添加 避免编辑后传入id等数据
          delete this.form.id;
          this.form.catelogId = this.categoryId;
          ApiAttr.addAttr(this.form).then(res => {
            this.submitLoading = false;
            if (res.data.code == 201) {
              this.$Message.success("操作成功");
              this.modalSpecVisible = false;
            } else {
              this.$Message.error(res.data.message);
              this.modalSpecVisible = false;
            }
          })
        }
      });
    },
  },
  mounted() {
    this.init();
  },
};
</script>
<style lang="scss" scoped>
/deep/ .ivu-table-wrapper {
  overflow: auto;
}

.table {
  min-height: 100vh;
  height: auto;
}
</style>
